package com.wannengbang.cloudleader.homepage;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.ShareDetailsBean;
import com.wannengbang.cloudleader.homepage.adapter.ShareBonusDetailsListAdapter;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.homepage.model.IHomePageModel;
import com.wannengbang.cloudleader.utils.Dp2Px;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShareBonusDetailsActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_total_money1)
    TextView tvTotalMoney1;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.nestedScrollView)
    NestedScrollView nestedScrollView;
    @BindView(R.id.layout_title)
    LinearLayout layoutTitle;
    @BindView(R.id.view_line)
    View viewLine;
    private IHomePageModel homePageModel;
    private String id;
    private String money;
    private String date;
    private ShareBonusDetailsListAdapter listAdapter;
    private List<ShareDetailsBean.DataBean.ItemListBean> beanList;
    private boolean isSetBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_bonus_details);
        ButterKnife.bind(this);
        getImmersionBar().statusBarDarkFont(false).statusBarDarkFont(false, 0.2f).init();
        initView();
        initData();
    }

    public void initView() {
        homePageModel = new HomePageModelImpl();
        id = getIntent().getStringExtra("id");
        money = getIntent().getStringExtra("money");
        date = getIntent().getStringExtra("date");
        beanList = new ArrayList<>();
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        listAdapter = new ShareBonusDetailsListAdapter(beanList);
        recyclerView.setAdapter(listAdapter);
        tvTotalMoney1.setText(money);
        titleBar.setTitle("分红详情(" + date + ")");

        int height = (int) Dp2Px.dp2Px(181);
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY <= 0) {   //设置标题的背景颜色
                    layoutTitle.setBackgroundColor(Color.argb((int) 0, 255, 255, 255));
                    viewLine.setBackgroundColor(Color.argb((int) 0, 243, 243, 243));
                    titleBar.setTitleColor(getResources().getColor(R.color.white));
                    titleBar.setLeftIcon(R.mipmap.ic_back_white);
                    getImmersionBar().statusBarDarkFont(false).statusBarDarkFont(false, 0.2f).init();
                    isSetBar = false;
                } else if (scrollY > 0 && scrollY <= height) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
                    float scale = (float) scrollY / height;
                    float alpha = (255 * scale);
                    layoutTitle.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
                    viewLine.setBackgroundColor(Color.argb((int) alpha, 243, 243, 243));
                } else {    //滑动到banner下面设置普通颜色
                    if (!isSetBar) {
                        layoutTitle.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
                        viewLine.setBackgroundColor(Color.argb((int) 255, 243, 243, 243));
                        titleBar.setTitleColor(getResources().getColor(R.color.text_color_2));
                        titleBar.setLeftIcon(R.mipmap.ic_back_black);
                        getImmersionBar().statusBarDarkFont(true).statusBarDarkFont(true, 0.2f).init();
                        isSetBar = true;
                    }
                }
            }
        });
    }

    public void initData() {
        homePageModel.requestDirectorBonusesInfo(id, new DataCallBack<ShareDetailsBean>() {
            @Override
            public void onSuccessful(ShareDetailsBean bean) {
                beanList.addAll(bean.getData().getItemList());
                listAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
}
